package com.qdsg.ysg.doctor.ui.adapter;

import android.animation.ObjectAnimator;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.shape.view.ShapeTextView;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseApplication;
import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.ImageLoaderHelper;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.response.ChatResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

/**
 * @Description: java类作用描述
 * @Author: lzp
 * @CreateDate: 2023/6/19 14:27
 * @UpdateRemark: 更新说明
 */
public class ChatAdapter extends BaseDelegateMultiAdapter<ChatResponse.Message, BaseViewHolder> {

    public static final String PLAYLOAD_SPEAKING = "isSpeaking";
    public static final String PLAYLOAD_PAUSE = "pause";
    public static final String PLAYLOAD_SENDSUCCESS = "sendSuccess";

    private String currentTime = " ";

    public ChatAdapter() {
        super();

        // 第一步，设置代理
        setMultiTypeDelegate(new BaseMultiTypeDelegate<ChatResponse.Message>() {
            @Override
            public int getItemType(@NotNull List<? extends ChatResponse.Message> data, int position) {
                return data.get(position).messageType;
            }
        });
        // 第二部，绑定 item 类型
        Objects.requireNonNull(getMultiTypeDelegate())
                .addItemType(ChatResponse.TEXT, R.layout.item_chat_text)
                .addItemType(ChatResponse.IMAGE, R.layout.item_chat_image)
                .addItemType(ChatResponse.AUDIO, R.layout.item_chat_audio)
                .addItemType(ChatResponse.PRESCRIPTION, R.layout.item_chat_diagnose)
                .addItemType(ChatResponse.CHECKOUT, R.layout.item_chat_checkout);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull ChatResponse.Message item) {
        if (helper.getItemViewType() != ChatResponse.PRESCRIPTION && helper.getItemViewType() != ChatResponse.CHECKOUT) {
            if (item.fromType == 1) {
                helper.getView(R.id.container_chat_item).setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
                if (!UtilString.isEmpty(BaseApplication.avatar)) {
                    ImageLoaderHelper.getInstance().GlideCircleImageLoader(getContext(), BaseApplication.avatar, helper.getView(R.id.img_head));
                } else {
                    if ("女".equals(BaseApplication.sex)) {
                        helper.setImageDrawable(R.id.img_head, ContextCompat.getDrawable(getContext(), R.mipmap.icon_girl));
                    } else {
                        helper.setImageDrawable(R.id.img_head, ContextCompat.getDrawable(getContext(), R.mipmap.icon_boy));
                    }
                }
            } else {
                helper.getView(R.id.container_chat_item).setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
                //加载患者头像
                if (!UtilString.isEmpty(BaseApplication.patientAvatar)) {
                    ImageLoaderHelper.getInstance().GlideCircleImageLoader(getContext(), BaseApplication.patientAvatar, helper.getView(R.id.img_head));
                } else if ("女".equals(BaseApplication.patientSex)) {
                    helper.setImageDrawable(R.id.img_head, ContextCompat.getDrawable(getContext(), R.mipmap.icon_girl));
                } else {
                    helper.setImageDrawable(R.id.img_head, ContextCompat.getDrawable(getContext(), R.mipmap.icon_boy));
                }
            }

            if (item.flag == 0) {
                helper.setGone(R.id.img_loading, true);
            } else if (item.flag == 1) {
                helper.setGone(R.id.img_loading, false);
                startLoading(helper.getView(R.id.img_loading));
            }
        }

        if (currentTime.equals(item.sendTime)) {
            helper.setGone(R.id.tv_time, true);
        } else {
            helper.setText(R.id.tv_time, item.sendTime);
            helper.setVisible(R.id.tv_time, true);
        }
        currentTime = item.sendTime == null ? " " : item.sendTime;

        switch (helper.getItemViewType()) {
            case ChatResponse.TEXT:
                helper.setText(R.id.tv_content, item.content);
                if (item.fromType == 1) {
                    helper.setTextColor(R.id.tv_content, ContextCompat.getColor(getContext(), R.color.white));
                    helper.setBackgroundResource(R.id.tv_content, R.drawable.shape_chat_green);
                } else {
                    helper.setTextColor(R.id.tv_content, ContextCompat.getColor(getContext(), R.color.mainGreen));
                    helper.setBackgroundResource(R.id.tv_content, R.drawable.shape_chat_white);
                }
                break;
            case ChatResponse.IMAGE:
                ImageLoaderHelper.getInstance().GlideImageLoader(getContext(), item.content, helper.getView(R.id.img));
                break;
            case ChatResponse.AUDIO:
                TextView audio = helper.getView(R.id.audio);
                audio.setText(item.time + "''");
                ViewGroup.LayoutParams layoutParams = audio.getLayoutParams();
                layoutParams.width = DpUtil.dip2px(getContext(), item.time * 3 + 80);
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                audio.setLayoutParams(layoutParams);
                if (item.fromType == 1) {
                    audio.setBackgroundResource(R.drawable.shape_chat_green);
                    Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.yuyin_white);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    audio.setCompoundDrawables(drawable, null, null, null);
                    audio.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                } else {
                    audio.setBackgroundResource(R.drawable.shape_chat_white);
                    Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.yuyin);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    audio.setCompoundDrawables(drawable, null, null, null);
                    audio.setTextColor(ContextCompat.getColor(getContext(), R.color.mainGreen));
                }
                break;
            case ChatResponse.PRESCRIPTION:
                if (item.prescription != null) {
                    helper.setGone(R.id.tv_null, true);
                    helper.setText(R.id.tv_diagnose_status, item.prescription.pFlagStr);

                    helper.setGone(R.id.tv_diagnose_time, TextUtils.isEmpty(item.prescription.passTime));
                    helper.setText(R.id.tv_diagnose_time, item.prescription.passTime);

                    helper.setText(R.id.tv_diagnose, "医生意见：" + (item.prescription.diagnoseResult == null ? "无" : item.prescription.diagnoseResult));

                    if (item.prescription.westernList != null) {
                        MedicineChatAdapter adapter = new MedicineChatAdapter(getContext(), item.prescription.westernList);
                        RecyclerView recyclerView = helper.getView(R.id.recyclerView);
                        recyclerView.setAdapter(adapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    }
                } else {
                    helper.setGone(R.id.tv_null, false);
                }
                break;

            case ChatResponse.CHECKOUT:
                ShapeTextView shapeTextView = helper.getView(R.id.tv_order_status);
                // 状态: 1-待支付 2-已支付 3-申请退费 4-已退费 5已撤销 6-已取消
                int status = item.checkList.status;
                if (status == 1) {
                    shapeTextView.setText("待支付");
                    shapeTextView.getShapeDrawableBuilder().setSolidColor(ContextCompat.getColor(getContext(), R.color.light_orange)).intoBackground();
                    shapeTextView.getTextColorBuilder().setTextColor(ContextCompat.getColor(getContext(), R.color.color_orange)).intoTextColor();
                } else if (status == 2) {
                    shapeTextView.setText("已支付");
                    shapeTextView.getShapeDrawableBuilder().setSolidColor(ContextCompat.getColor(getContext(), R.color.light_bule)).intoBackground();
                    shapeTextView.getTextColorBuilder().setTextColor(ContextCompat.getColor(getContext(), R.color.mainBlue)).intoTextColor();
                } else if (status == 3) {
                    shapeTextView.setText("退费中");
                    shapeTextView.getShapeDrawableBuilder().setSolidColor(ContextCompat.getColor(getContext(), R.color.light_red)).intoBackground();
                    shapeTextView.getTextColorBuilder().setTextColor(ContextCompat.getColor(getContext(), R.color.color_F54C4C)).intoTextColor();
                } else {
                    if (status == 4) {
                        shapeTextView.setText("已退费");
                    } else if (status == 5) {
                        shapeTextView.setText("已撤销");
                    } else if (status == 6) {
                        shapeTextView.setText("已取消");
                    }
                    shapeTextView.getShapeDrawableBuilder().setSolidColor(ContextCompat.getColor(getContext(), R.color.light_gray)).intoBackground();
                    shapeTextView.getTextColorBuilder().setTextColor(ContextCompat.getColor(getContext(), R.color.white)).intoTextColor();
                }
                helper.setText(R.id.tv_diagnose, item.checkList.diagnoseName);
                helper.setText(R.id.tv_doctor_advice, item.checkList.diagnoseResult);
                if (item.checkList.jcList != null && item.checkList.jcList.size() > 0) {
                    RecyclerView rvJc = helper.getView(R.id.rv_jc);
                    rvJc.setLayoutManager(new LinearLayoutManager(getContext()));
                    CheckItemAdapter adapter = new CheckItemAdapter(item.checkList.jcList);
                    rvJc.setAdapter(adapter);
                } else {
                    helper.getView(R.id.tv_jc).setVisibility(View.GONE);
                    helper.getView(R.id.header_jc).setVisibility(View.GONE);
                }
                if (item.checkList.jyList != null && item.checkList.jyList.size() > 0) {
                    RecyclerView rvJy = helper.getView(R.id.rv_jy);
                    rvJy.setLayoutManager(new LinearLayoutManager(getContext()));
                    CheckItemAdapter adapter = new CheckItemAdapter(item.checkList.jyList);
                    rvJy.setAdapter(adapter);
                } else {
                    helper.getView(R.id.tv_jy).setVisibility(View.GONE);
                    helper.getView(R.id.header_jy).setVisibility(View.GONE);
                }
                helper.setGone(R.id.tv_time, true);
                break;

            default:
                break;
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ChatResponse.Message item, @NotNull List<?> payloads) {
        if (payloads.isEmpty()) {
            super.convert(holder, item, payloads);
            return;
        }
        for (Object payload : payloads) {
            switch (String.valueOf(payload)) {
                case PLAYLOAD_SPEAKING:
                    holder.setTextColor(R.id.audio, ContextCompat.getColor(getContext(), R.color.zm_red));
                    break;
                case PLAYLOAD_PAUSE:
                    if (item.fromType == 1) {
                        holder.setTextColor(R.id.audio, ContextCompat.getColor(getContext(), R.color.white));
                    } else {
                        holder.setTextColor(R.id.audio, ContextCompat.getColor(getContext(), R.color.mainGreen));
                    }
                    break;
                case PLAYLOAD_SENDSUCCESS:
                    holder.setGone(R.id.img_loading, true);
                    break;

                default:
                    break;
            }
        }
    }

    private void startLoading(View view) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);

        // 动画的持续时间，执行多久？
        anim.setDuration(1000);
        anim.setRepeatCount(-1);

        // 回调监听
        anim.addUpdateListener(animation -> {
            float value = (Float) animation.getAnimatedValue();
        });

        // 正式开始启动执行动画
        anim.start();
    }
}